define(['app', 'assets/js/zTree', 'assets/js/zTree.excheck'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
    $scope.form = {};
    var currentNode = null;
    var menuTreeObj;
    var tId = "";
    
	$scope.form.fk_client =params.fk_client;
	$scope.form.pk_app =params.fk_client;
	$scope.form.fk_roleid = params.fk_roleid;
	
	var queryButton = function() {
		httpService.post(app.urls.button_selectByApp, $scope.form, function(data) {
			if("0000" === data.code){
				$scope.buttons = data.data;
				$scope.$apply();
				for(var i = 0; i < $scope.buttons.length; i++) {
                    if($scope.buttons[i].is_checked) {
                        $('#button_'+$scope.buttons[i].id).prop("checked", true);
                    }
                }
			}else{
				alert(data.msg);
			}
		});
	}
	
    //单击
	var checkedList = [];
    var zTreeOnClick = function(event, treeId, treeNode) {
    	var nodes = menuTreeObj.transformToArray(menuTreeObj.getNodes());
    	for (var i = 0; i < nodes.length; i++) {
        	if(nodes[i].checked) {
        		$(".menu_" + nodes[i].id).prop("checked", true);
        	}else{
        		$(".menu_" + nodes[i].id).prop("checked", false);
        	}
		}
    };

    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        view:{
        	selectedMulti: false
        },
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: {"Y": "ps", "N": "ps"}
		},
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: zTreeOnClick
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）

    //查询菜单
    var queryMenu = function(){
        httpService.post(app.urls.menu_select, $scope.form, function(data){
            if("0000" === data.code) {
            	$scope.menus = data.data;
            	$scope.$apply();
				for(var i = 0; i < $scope.menus.length; i++) {
                    if($scope.menus[i].is_checked) {
                        $('#menu_'+$scope.menus[i].id).prop("checked", true);
                    }
                }
				
                var zNodes =  [];
                for (var i = 0; i < data.data.length; i++) {
                    zNodes[i] = {};
                    if(data.data[i].father_id == 'ROOT') {
                    	data.data[i].father_id = 0;
                    }
                    zNodes[i].id = data.data[i].id;
                    zNodes[i].pId = data.data[i].father_id;
                    zNodes[i].name = data.data[i].name;
                    zNodes[i].open = true;
                    zNodes[i].checked = data.data[i].is_checked;
                }
                
                menuTreeObj = $.fn.zTree.init($("#menuTree"), setting, zNodes);
                $scope.$apply();
                
                queryButton();
            }else{
                alert(data.msg);
            }
        });
    }
    queryMenu();
    
    //添加或删除角色按钮
    $scope.propButton = function(obj) {
    	if($("#button_" + obj.id).is(':checked')) {
    		$("#button_" + obj.id).prop("checked", true);
    		$("#menu_" + obj.fk_menuid).prop("checked", true);
    		
        	var nodes = menuTreeObj.transformToArray(menuTreeObj.getNodes());
        	for (var i = 0; i < nodes.length; i++) {
            	if(nodes[i].id == obj.fk_menuid) {
            		menuTreeObj.checkNode(nodes[i], true, true);
            	}
    		}
    	}else{
    		$("#button_" + obj.id).prop("checked", false);
    	}
    }
    
    //添加或删除角色菜单
    $scope.propMenu = function(obj) {
    	if($("#menu_" + obj.id).is(':checked')) {
    		$(".menu_" + obj.id).prop("checked", true);
    		
        	var nodes = menuTreeObj.transformToArray(menuTreeObj.getNodes());
        	for (var i = 0; i < nodes.length; i++) {
            	if(nodes[i].id == obj.id) {
            		menuTreeObj.checkNode(nodes[i], true, true);
            	}
    		}
    	}else{
    		$(".menu_" + obj.id).prop("checked", false);
    	}
    }
    
    
	//返回应用
	$scope.goback=function(){
		var menu = {"url":"modules/content/usermanagement/userRole/list?FatherControllerName="+$scope.controllerName+"&type="+params.type+"&fk_roleid="+params.fk_roleid+"&fk_groupid="+params.fk_groupid};
		eventBusService.publish("modules.content.load",menu)
	}
	
    //保存
    $scope.add = function(){
		$scope.deleteButton();
	}
	
	$scope.deleteButton = function(){
		$scope.form.type = '3';
		httpService.post(app.urls.role_power_delete,$scope.form,function(data){
			if("0000" === data.code){
				$scope.deleteMenu();				
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	$scope.deleteMenu = function(){
		$scope.form.type = '2';
		httpService.post(app.urls.role_power_delete,$scope.form,function(data){
			if("0000" === data.code){
				$scope.addMenuAndButton();
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	$scope.addMenuAndButton = function(){
		var menuvalues = [];
		var buttonvalues = [];
		var buttonmenus = [];
    	
		$('#'+$scope.controllerName+' input[name="dataPk_menu"]:checked').each(function(){ 
			menuvalues.push($(this).val());
    	});
		
		$('#'+$scope.controllerName+' input[name="dataPk_button"]:checked').each(function(){ 
			buttonvalues.push($(this).val().split(',')[0]);
			buttonmenus.push($(this).val().split(',')[1]);
    	});
		
    	if(menuvalues.length < 1 && buttonvalues.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择添加数据。"});
    	}else{
    		$scope.form.menuvalues = JSON.stringify(menuvalues);
    		$scope.form.buttonvalues = JSON.stringify(buttonvalues);
    		$scope.form.buttonmenus = JSON.stringify(buttonmenus);

    		httpService.post(app.urls.role_power_all_insert,$scope.form,function(data){
				if(1 === data){
    				alert("保存成功");
        		}else{
        			alert("保存失败");
        		}
    		});
    		
    	}
	}
}}});